package com.xmread.cn.network;

import com.xmread.cn.bean.CommonResult;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

public final class RxSchedulerUtils {
    public static <T> ObservableTransformer<T, T> normalSchedulersTransformer() {
        return new ObservableTransformer<T, T>() {
            public ObservableSource<T> apply(Observable<T> observable) {
                return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public static <T> ObservableTransformer<CommonResult<T>, T> handleResult() {
        return new ObservableTransformer<CommonResult<T>, T>() {
            public ObservableSource<T> apply(Observable<CommonResult<T>> observable) {
                return observable.flatMap(new Function<CommonResult<T>, ObservableSource<T>>() {
                    public ObservableSource<T> apply(CommonResult<T> commonResult) throws Exception {
                        if (commonResult.getCode() == 200) {
                            return RxSchedulerUtils.createData(commonResult.getData());
                        }
                        return Observable.error((Throwable) new ServerException(commonResult.getMsg()));
                    }
                });
            }
        };
    }

    public static <T> ObservableTransformer<CommonResult<T>, T> handleResultToMainThread() {
        return new ObservableTransformer<CommonResult<T>, T>() {
            public ObservableSource<T> apply(Observable<CommonResult<T>> observable) {
                return observable.flatMap(new Function<CommonResult<T>, ObservableSource<T>>() {
                    public ObservableSource<T> apply(CommonResult<T> commonResult) throws Exception {
                        if (commonResult.getCode() == 200) {
                            return RxSchedulerUtils.createData(commonResult.getData());
                        }
                        return Observable.error((Throwable) new ServerException(commonResult.getMsg()));
                    }
                }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    public static <T> Observable<T> createData(final T t) {
        return Observable.create(new ObservableOnSubscribe<T>() {
            public void subscribe(ObservableEmitter<T> observableEmitter) throws Exception {
                try {
                    observableEmitter.onNext(t);
                    observableEmitter.onComplete();
                } catch (Exception e) {
                    observableEmitter.onError(e);
                }
            }
        });
    }
}
